package cn.tedu.yushouge.pet.adopt.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetAddNewParam;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetPicAddNewParam;
import cn.tedu.yushouge.pet.adopt.pojo.param.PetUpdateInfoParam;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetListItemVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetPicListItemVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetPicStandardVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.PetStandardVO;
import cn.tedu.yushouge.pet.adopt.service.IPetPicService;
import cn.tedu.yushouge.pet.adopt.service.IPetService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/picture/")
@Api(tags = "03. 宠物图片管理模块")
@Slf4j
@Validated
public class PetPicController {

    @Autowired
    IPetPicService petPicService;

    //http://localhost:9818/pet/add-new
    @PostMapping("add-new")
    @ApiOperation("添加宠物图片")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid PetPicAddNewParam petPicAddNewParam,
                             Long petId) {
        log.debug("开始处理【添加宠物】的请求，参数：{},{}", petPicAddNewParam,petId);
        petPicService.addNew(petPicAddNewParam,petId);
        return JsonResult.ok();
    }
    //http://localhost:9818/pet/delete
    @PostMapping("delete")
    @ApiOperation("根据ID删除宠物图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除宠物失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除宠物】的请求，参数：{}", id);
        petPicService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping ("setCover")
    @ApiOperation("根据ID将图片设置为封面")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setCover(@Range(min = 1, message = "请提交有效的图片ID值！")
                                     @RequestParam Long id)  {
        log.debug("开始处理【根据ID将图片设置为封面】的请求，参数：{}", id);
        petPicService.setCover(id);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询图片详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "long")
    })
    public JsonResult<PetPicStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询图片详情失败，请提交合法的ID值！")
                                                         @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询图片详情】的请求，参数：{}", id);
        PetPicStandardVO result = petPicService.getStandardById(id);
        return JsonResult.ok(result);
    }



    //http://localhost:9818/pet/list
    @GetMapping("listByPetId")
    @ApiOperation("根据宠物ID查询宠物图片")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<PetPicListItemVO>> listByPetId(@Range(min = 1, message = "查询宠物图片失败，请提供正确的宠物ID值！")
                               @RequestParam Long petId) {
        log.debug("开始处理【根据ID删除宠物】的请求，参数：{}", petId);
        List<PetPicListItemVO> list = petPicService.getPicListByPetId(petId);
        return JsonResult.ok(list);
    }

    @GetMapping("listForIsCover")
    @ApiOperation("查询宠物图片封面")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<PetPicListItemVO>> listForIsCover() {
        log.debug("开始处理【查询宠物图片封面】的请求");
        List<PetPicListItemVO> list = petPicService.getPicListForIsCover();
        return JsonResult.ok(list);
    }


}
